/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 * Date: 2013.02.11
 */

#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

const int maxN = 5003;

int bn, n, k, a[maxN], b[maxN], sa[maxN], sb[maxN];
int cc, f[2][maxN], c[maxN][maxN];

inline void relax( int &a, int b )
{
  a = min(a, b);
}

int main()
{
  assert(scanf("%d%d", &n, &k) == 2 && k <= n && n <= maxN);
  forn(i, n)
    assert(scanf("%d", &a[i]) == 1);
  sort(a, a + n);
  for (int i = 0, j = 0; i < n; i = j)
  {
    while (j < n && a[i] == a[j])
      j++;
    a[bn] = a[i], b[bn++] = j - i;
  }

  forn(i, bn)
  {
    sb[i + 1] = sb[i] + b[i];
    sa[i + 1] = sa[i] + a[i] * b[i];
  }
  for (int r = 1; r <= bn; r++)
  {
    int m = r - 1;
    for (int l = r - 1; l >= 0; l--)
    {
      while (m - 1 >= l && 2 * (sb[m] - sb[l]) >= sb[r] - sb[l])
        m--;
      c[r][l] = a[m] * ((sb[m] << 1) - sb[l] - sb[r]) - (sa[m] << 1) + sa[l] + sa[r];
    }
  }

  memset(f[cc], 0x3F, sizeof(f[cc]));
  f[cc][0] = 0;
  int val = (n / k + 1) * 10, ans = (int)1e9;
  forn(j, k)
  {
    memset(f[cc ^ 1], 0x3F, sizeof(f[cc]));
    forn(i, bn)
      for (int r = i + 1; r <= bn && r < i + val; r++)
        relax(f[cc ^ 1][r], f[cc][i] + c[r][i]);
    cc ^= 1;
    relax(ans, f[cc][bn]);
  }
  printf("%d\n", ans);
  return 0;
}
